/**
 * 
 */
package com.zito.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.downlove.common.LinkTool;
import com.downlove.common.Storage.FileInfo;
import com.downlove.common.Storage.ImageInfo;
import com.downlove.template.freemarker.FreeMarkerEngine;
import com.zito.manager.ArticlefilesManager;
import com.zito.manager.ArticlephotosManager;
import com.zito.manager.ContributeManager;
import com.zito.manager.ElectronicfilesManager;
import com.zito.manager.ElectronicManager;
import com.zito.manager.InteractSession;
import com.zito.chat.ChatServlet;
import com.zito.manager.InteractManager;

/**
 * @author YunHai-Home
 * 
 */
public class InteractAction {
	private String message = "";
	private String jumpUrl = "";

	/**
	 * 退出登录，返回登录界面
	 * 
	 * @param request
	 * @param response
	 */
	public void loginOut(HttpServletRequest request, HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		boolean re = true;
		String message = "登出成功！";
		try {
			InteractSession.get().clearLogin();
		} catch (Exception e) {
			re = false;
			e.printStackTrace();
		}
		fm.put("success", re);
		fm.put("message", message);
		FreeMarkerEngine.action(fm, "submit.html");
	}

	/**
	 * 管理员登录
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public boolean login(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		InteractManager obj = new InteractManager();
		boolean re = false;
		try {
			re = obj.authAdmin();
		} catch (Exception e) {
			message = e.getMessage();
		}
		jumpUrl = "/interact/index";
		fm.put("success", re);
		fm.put("message", message);
		fm.put("jump", jumpUrl);
		FreeMarkerEngine.action(fm, "submit.html");
		return true;
	}
	
	/**
	 * 在线交流
	 * @param request
	 * @param response
	 */
	public void ChatServlet(HttpServletRequest request,
			HttpServletResponse response) {
		ChatServlet obj = new ChatServlet();
		try {
			obj.doPost(request,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 电子台账编辑
	 * @param request
	 * @param response
	 * @return
	 */
	public boolean ElectronicEdit(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		ElectronicManager obj = new ElectronicManager();
		boolean re = false;
		message = "电子台账发布成功，您可以清空表单后继续添加新的电子台账";
		try {
			re = obj.operatingPost();
		} catch (Exception e) {
			message = e.getMessage();
		}
		fm.put("success", re);
		fm.put("message", message);
		FreeMarkerEngine.action(fm, "submit.html");
		return true;
	}
	
	/**
	 * 电子台账文件上传
	 * @param request
	 * @param response
	 * @return
	 */
	public boolean  ElectronicFileUpload(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		ElectronicfilesManager obj = new  ElectronicfilesManager();
		boolean re = false;
		message = "附件上传成功";
		Long tmpFileID = -1L;
		try {
			Map<String, Object> tmpInfo = obj.uploadFiles();
			tmpFileID = (Long) tmpInfo.get("tmpFileid");
			FileInfo info = (FileInfo) tmpInfo.get("info");
			if (info != null) {
				fm.put("filenames", info.getFileName());
				fm.put("originalnames", info.getFileOriginalName());
				fm.put("filepath", LinkTool.files(info.getFilePath()));
			}
			re = (info == null) ? false : true;
		} catch (Exception e) {
			message = e.getMessage();
		}
		fm.put("success", re);
		fm.put("message", message);
		fm.put("fileid", tmpFileID);
		FreeMarkerEngine.action(fm, "uploadfile.html");
		return true;
	}
	
	/**
	 * 电子台账删除
	 * @param request
	 * @param response
	 * @return
	 */
	public boolean DeleteElectronic(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		ElectronicManager obj = new ElectronicManager();
		boolean re = false;
		message = "文章删除成功！";
		try {
			re = obj.deleteElectronic();
		} catch (Exception e) {
			message = e.getMessage();
		}
		fm.put("success", re);
		fm.put("message", message);
		FreeMarkerEngine.action(fm, "submit.html");
		return true;
	}
	
	/** 
	 * 删除电子台账附件
	 * @param request
	 * @param response
	 * @return
	 */
	public boolean deleteElectronicFile(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		ElectronicfilesManager obj = new ElectronicfilesManager();
		boolean re = false;
		message = "附件删除成功";
		try {
			re = obj.deleteOneArticleFile();
		} catch (Exception e) {
			message = e.getMessage();
		}
		fm.put("success", re);
		fm.put("message", message);
		FreeMarkerEngine.action(fm, "submit.html");
		return true;
	}

	/**
	 * 上传文章图片
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public void ArticleImageUpload(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		ArticlephotosManager obj = new ArticlephotosManager();
		boolean re = false;
		message = "图片上传成功";
		Long tmpFileID = -1L;
		try {
			Map<String, Object> tmpInfo = obj.uploadImages();
			tmpFileID = (Long) tmpInfo.get("tmpFileid");
			ImageInfo info = (ImageInfo) tmpInfo.get("info");
			if (info != null) {
				fm.put("filenames", info.getImageName());
				fm.put("originalnames", info.getImageOriginalName());
				fm.put("filepath", LinkTool.images(info.getImagePath()));
			}
			re = (info == null) ? false : true;
		} catch (Exception e) {
			message = e.getMessage();
			if (message == null || "".equals(message))
				message = "上传图片出现未知错误，请及时联系管理人员！";
		}
		fm.put("success", re);
		fm.put("message", message);
		fm.put("fileid", tmpFileID);

		FreeMarkerEngine.action(fm, "uploadfile.html");
	}

	/**
	 * 上传文章附件
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public void ArticleFileUpload(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		ArticlefilesManager obj = new ArticlefilesManager();
		boolean re = false;
		message = "附件上传成功";
		Long tmpFileID = -1L;
		try {
			Map<String, Object> tmpInfo = obj.uploadFiles();
			tmpFileID = (Long) tmpInfo.get("tmpFileid");
			FileInfo info = (FileInfo) tmpInfo.get("info");
			if (info != null) {
				fm.put("filenames", info.getFileName());
				fm.put("originalnames", info.getFileOriginalName());
				fm.put("filepath", LinkTool.files(info.getFilePath()));
			}
			re = (info == null) ? false : true;
		} catch (Exception e) {
			message = e.getMessage();
		}
		fm.put("success", re);
		fm.put("message", message);
		fm.put("fileid", tmpFileID);

		FreeMarkerEngine.action(fm, "uploadfile.html");
	}

	/**
	 * 删除文章中的一个图片
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public void deleteArticlePhoto(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		ArticlephotosManager obj = new ArticlephotosManager();
		boolean re = false;
		message = "图片删除成功";
		try {
			re = obj.deleteOneArticlePhoto();
		} catch (Exception e) {
			message = e.getMessage();
		}
		fm.put("success", re);
		fm.put("message", message);
		FreeMarkerEngine.action(fm, "submit.html");
	}

	/**
	 * 删除文章中的一个附件
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public void deleteArticleFile(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		ArticlefilesManager obj = new ArticlefilesManager();
		boolean re = false;
		message = "附件删除成功";
		try {
			re = obj.deleteOneArticleFile();
		} catch (Exception e) {
			message = e.getMessage();
		}
		fm.put("success", re);
		fm.put("message", message);
		FreeMarkerEngine.action(fm, "submit.html");
	}
	
	/**
	 * 保存 在线投稿
	 * @param request
	 * @param response
	 * @return
	 */
	public void SaveContributeEdit(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		ContributeManager obj = new ContributeManager();
		boolean re = false;
		message = "投稿文章发布成功，您可以清空表单后继续添加新的投稿！";
		try {
			re = obj.operatingPost();
		} catch (Exception e) {
			message = e.getMessage();
		}
		fm.put("success", re);
		fm.put("message", message);
		FreeMarkerEngine.action(fm, "submit.html");
	}

	
	/**
	 * 删除投稿
	 * @param request
	 * @param response
	 * @return
	 */
	public void DeleteContribute(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		ContributeManager obj = new ContributeManager();
		boolean re = false;
		message = "删除成功！";
		try {
			re = obj.ContributeOnLineDelete();
		} catch (Exception e) {
			message = e.getMessage();
		}
		fm.put("success", re);
		fm.put("message", message);
		FreeMarkerEngine.action(fm, "submit.html");
	}
	

	
	/**
	 * 获取信息刊用排行榜列表
	 * @param request
	 * @param response
	 * @return
	 */
	public void GetContributeRankList(HttpServletRequest request,
			HttpServletResponse response) {
		FreeMarkerEngine fm = new FreeMarkerEngine(request, response);
		ContributeManager obj = new ContributeManager();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = obj.getContributeRankList();
		} catch (Exception e) {
			message = e.getMessage();
		}
		fm.put("manager", obj);
		fm.put("ListData", list);
		FreeMarkerEngine.action(fm, "interact/data/ContributeRankList.ftl");
	}
}
